//
//  XJLChatController.m
//  XJL_WeChat
//
//  Created by XJL on 16/8/15.
//  Copyright © 2016年 Xujiali. All rights reserved.
//

#import "XJLChatController.h"
#import "EaseEmotionManager.h"
#import "EaseEmoji.h"
#import "XJLSendRedPacketViewController.h"

@interface XJLChatController()<EaseMessageViewControllerDelegate>

@end
@implementation XJLChatController

-(void)viewDidLoad{
    
    [super viewDidLoad];
    dispatch_async(dispatch_get_main_queue(), ^{
        
        // 设置是否显示刷新控制
        self.showRefreshHeader = YES;
        self.delegate = self;
        // 显示聊天的用户名
        self.title = self.conversation.chatter;
        XJLLog(@"%@",self.title);
        // 1.设置cell的背景图片
        [self setupCellBg];
        
        // 2.通过会话管理者获取已收发消息
        [self tableViewDidTriggerHeaderRefresh];
        
        //3.添加一个红包按钮
        [self.chatBarMoreView insertItemWithImage:[UIImage imageNamed:@"redbag_icon"] highlightedImage:[UIImage imageNamed:@"redbag_icon"] title:@"红包"];
    });
}

#pragma mark 监听红包按钮的点击
-(void)moreView:(EaseChatBarMoreView *)moreView didItemInMoreViewAtIndex:(NSInteger)index{
    
    if (index == 6) {
        XJLSendRedPacketViewController *srpVc = [[XJLSendRedPacketViewController alloc]init];
        
        srpVc.didSendRedPacket = ^(double amount,NSInteger count){
            XJLLog(@"红包金额:%.2f 红包个数:%zd",amount,count);
            NSDictionary *redPackerInfo = @{
                                            @"RedPacketID":@"A001",
                                            @"amount":@(amount),
                                            @"type":@(0)};
        [self sendTextMessage:@"红包消息" withExt:redPackerInfo];
        };
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:srpVc];
        srpVc.hidesBottomBarWhenPushed = YES;
        [self presentViewController:nav animated:YES completion:nil];
    }
}

#pragma mark 设置cell的背景图片
- (void)setupCellBg {
    [[EaseBaseMessageCell appearance] setSendBubbleBackgroundImage:[[UIImage imageNamed:@"EaseUIResource.bundle/chat_sender_bg"] stretchableImageWithLeftCapWidth:5 topCapHeight:35]];
    [[EaseBaseMessageCell appearance] setRecvBubbleBackgroundImage:[[UIImage imageNamed:@"EaseUIResource.bundle/chat_receiver_bg"] stretchableImageWithLeftCapWidth:35 topCapHeight:35]];
    
    // 设置语音的图片
    [[EaseBaseMessageCell appearance] setSendMessageVoiceAnimationImages:@[[UIImage imageNamed:@"chat_sender_audio_playing_full"], [UIImage imageNamed:@"chat_sender_audio_playing_000"], [UIImage imageNamed:@"chat_sender_audio_playing_001"], [UIImage imageNamed:@"chat_sender_audio_playing_002"], [UIImage imageNamed:@"chat_sender_audio_playing_003"]]];
    [[EaseBaseMessageCell appearance] setRecvMessageVoiceAnimationImages:@[[UIImage imageNamed:@"chat_receiver_audio_playing_full"],[UIImage imageNamed:@"chat_receiver_audio_playing000"], [UIImage imageNamed:@"chat_receiver_audio_playing001"], [UIImage imageNamed:@"chat_receiver_audio_playing002"], [UIImage imageNamed:@"chat_receiver_audio_playing003"]]];
    
    // 设置头像的圆角
    [[EaseBaseMessageCell appearance] setAvatarSize:40.f];
    [[EaseBaseMessageCell appearance] setAvatarCornerRadius:20.f];
    
    // 设置表情数据
    EaseEmotionManager *manager= [[EaseEmotionManager alloc] initWithType:EMEmotionDefault emotionRow:3 emotionCol:7 emotions:[EaseEmoji allEmoji]];
    [self.faceView setEmotionManagers:@[manager]];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIAlertController *alertVc = [[UIAlertController alloc]init];
    alertVc.view.bounds = CGRectMake(0, 0, 100, 100);
    alertVc.view.backgroundColor = [UIColor redColor];
}

//获取自定义消息的cell
-(UITableViewCell *)messageViewController:(UITableView *)tableView cellForMessageModel:(id<IMessageModel>)messageModel{
    //如果cell返回空，还是用环信自己创建的cell
    EMMessage *message = [messageModel message];
    NSString *redPacketID = message.ext[@"redPacketID"];
    if (redPacketID.length > 0) {
        XJLLog(@"这是红包消息的cell %@",redPacketID);
        
    }
    return nil;
}

//获取自定义cell的高度
-(CGFloat)messageViewController:(EaseMessageViewController *)viewController heightForMessageModel:(id<IMessageModel>)messageModel withCellWidth:(CGFloat)cellWidth{
    
    //如果高度返回了,高度会是环信自己计算
    // 如果cell返回空,还是用环信自己创建的cell
    EMMessage *message = [messageModel message];
    NSString *redPacketID = message.ext[@"redPacketID"];
    if (redPacketID.length > 0) {
        XJLLog(@"这是红包消息的cell %@",redPacketID);
        
    }
    return 0;
}
@end
